

//
//  ZZHomeSecView.m
//  新项目
//
//  Created by 刘耀宗 on 2016/12/30.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "ZZHomeSecView.h"
#import "SCChart.h"
#import "ZZDateModel.h"
#import "SCChartLabel.h"

@interface ZZHomeSecView ()<SCChartDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *lineView;


@end
@implementation ZZHomeSecView
-(UIScrollView *)scrollowView
{
    if (_scrollowView == nil) {
        _scrollowView = [[UIScrollView alloc] init];
        _scrollowView.delegate=self;
    }
    return _scrollowView;
    
}
+(instancetype)ViewWithXib
{
    ZZHomeSecView *view=[[[NSBundle mainBundle] loadNibNamed:@"ZZHomeSecView" owner:nil options:nil] lastObject];
    return view;
    
}
-(void)changeTheLabel
{
    __block    CGFloat chartCavanHeight = 150 - UULabelHeight*3;
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[SCChartLabel class]]) {
            [obj removeFromSuperview];
        }
    }];
    NSMutableArray *arr;
    arr=[ZZUserModel shareViewModel].secLabelArr;
      CGFloat levelHeight = chartCavanHeight /(arr.count-1); // 每个区间的高度
    [arr enumerateObjectsUsingBlock:^(SCChartLabel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SCChartLabel * label = [[SCChartLabel alloc] initWithFrame:CGRectMake(0.0,chartCavanHeight-idx*levelHeight+215, UUYLabelwidth, UULabelHeight)];
        label.text = obj.text; // 每
        [self addSubview:label];
    }];
}
-(SCChart *)chartView
{
    if (_chartView == nil) {
        _chartView = [[SCChart alloc] initwithSCChartDataFrame:CGRectMake(-25, 0, 0, 150)
                                                    withSource:self
                                                     withStyle:SCChartLineStyle];
    }
    return _chartView;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheLabel) name:@"changeTheLabel" object:nil];
    
    self.nowYueBtn.layer.cornerRadius=15;
    self.nowYueBtn.layer.masksToBounds=YES;
    self.nowZhoubtn.selected=YES;
    [self.nowZhoubtn setBackgroundColor:CcColor(50, 136, 236)];
    self.nowZhoubtn.layer.cornerRadius=15;
    self.nowZhoubtn.layer.masksToBounds=YES;
    [self addSubview:self.scrollowView];
    // self.scrollowView.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width *3, 150);
    
//    
//    if (self.chartView) {
//        [self.chartView removeFromSuperview];
//        self.chartView = nil;
//    }
//    self.chartView = [[SCChart alloc] initwithSCChartDataFrame:CGRectMake(-25, 0, 0, 150)
//                                                    withSource:self
//                                                     withStyle:SCChartLineStyle];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.carNumLabel.text]; // 改变特定范围颜色大小要用的
    NSRange r = NSMakeRange(3, self.carNumLabel.text.length-4);
    NSRange r2 =  NSMakeRange(3, self.carNumLabel.text.length-4);
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:r];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:r2];
    [self.carNumLabel setAttributedText:attributedString];
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:self.allNumLabel.text]; // 改变特定范围颜色大小要用的
    NSRange r3 = NSMakeRange(3, self.allNumLabel.text.length-4);
    NSRange r4 =  NSMakeRange(3, self.allNumLabel.text.length-4);
    
    [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:r3];
    [attributedString1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:r4];
    [self.allNumLabel setAttributedText:attributedString1];
    
    
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    NSLog(@"self.chartView.lineChart.flag=%ld",self.chartView.lineChart.flag);
    
}
-(void)setBesArr:(NSArray *)besArr
{
    _besArr=besArr;
    self.chartView.frame= CGRectMake(-25, 0, 40*besArr.count+UUYLabelwidth, 150);
    
    self.scrollowView.contentSize=CGSizeMake(40*besArr.count+UUYLabelwidth+20, 150);
    
}
-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    self.lineView.hidden=YES;
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //设置虚线颜色
    CGContextSetStrokeColorWithColor(currentContext, LineColor.CGColor);
    //设置虚线宽度
    CGContextSetLineWidth(currentContext, 2);
    //设置虚线绘制起点
    CGContextMoveToPoint(currentContext, self.lineView.frame.origin.x, self.lineView.frame.origin.y);
    //设置虚线绘制终点
    CGContextAddLineToPoint(currentContext, self.lineView.frame.origin.x,self.lineView.frame.origin.y + self.lineView.frame.size.height-30);
    //设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点
    CGFloat arr[] = {3, 1};
    //下面最后一个参数“2”代表排列的个数。
    CGContextSetLineDash(currentContext, 0, arr, 2);
    //画线
    CGContextDrawPath(currentContext, kCGPathStroke);
    
}
- (NSArray *)getXTitles:(int)num {
    NSMutableArray *xTitles = [NSMutableArray array];
    
    for (int i=0; i<self.besArr.count; i++) {
        ZZDateModel *model=self.besArr[i];
        model.dt=[model.dt substringFromIndex:5];
        NSString * str = [NSString stringWithFormat:@"%@",model.dt];
        [xTitles addObject:str];
    }
    return xTitles;
}

//查看本周的数据
- (IBAction)cliciForLookZhou:(id)sender {
    UIButton *btn=sender;
    if (!btn.selected) {
        btn.selected=!btn.selected;
        [btn setBackgroundColor:CcColor(50, 136, 236)];
        self.nowYueBtn.selected=NO;
        [self.nowYueBtn setBackgroundColor:CcColor(202, 202, 202)];
        if ([self.delegate respondsToSelector:@selector(lookBenZhou:)]) {
            [self.delegate lookBenZhou:self];
        }
    }
    
    
    
}
//查看本月的数据
- (IBAction)clickForLookYue:(id)sender {
    UIButton *btn=sender;
    if (!btn.selected) {
        btn.selected=!btn.selected;
        [btn setBackgroundColor:CcColor(50, 136, 236)];
        self.nowZhoubtn.selected=NO;
        [self.nowZhoubtn setBackgroundColor:CcColor(202, 202, 202)];
        if ([self.delegate respondsToSelector:@selector(lookBenYue:)]) {
            [self.delegate lookBenYue:self];
        }
    }
    
}

#pragma mark - @required
//横坐标标题数组
- (NSArray *)SCChart_xLableArray:(SCChart *)chart {
    return [self getXTitles:self.besArr.count];
}
//数值多重数组
- (NSArray *)SCChart_yValueArray:(SCChart *)chart {
    
    NSMutableArray *ary = [NSMutableArray array];
    for (NSInteger i = 0; i < self.besArr.count; i++) {
        ZZDateModel *model=self.besArr[i];
        CGFloat num = [model.Count floatValue];
        NSString *str = [NSString stringWithFormat:@"%f",num];
        NSLog(@"当前的y值是%@",str);
        [ary addObject:str];
        
        //                CGFloat num = arc4random_uniform(20);
        //                NSString *str = [NSString stringWithFormat:@"%f",num];
        //                NSLog(@"当前的y值是%@",str);
        //                [ary addObject:str];
    }
    return @[ary];
}

#pragma mark - @optional
//颜色数组
- (NSArray *)SCChart_ColorArray:(SCChart *)chart {
    return @[CcColor(50, 136, 236),SCRed,SCGreen];
}

#pragma mark 折线图专享功能
//标记数值区域
- (CGRange)SCChartMarkRangeInLineChart:(SCChart *)chart {
    return CGRangeZero;
}

//判断显示横线条
- (BOOL)SCChart:(SCChart *)chart ShowHorizonLineAtIndex:(NSInteger)index {
    return YES;
}

//判断显示最大最小值
- (BOOL)SCChart:(SCChart *)chart ShowMaxMinAtIndex:(NSInteger)index {
    return NO;
}

- (IBAction)clickForPushOrderCarDetail:(UIButton *)btn {
    
    
    if ([self.Secdelegate respondsToSelector:@selector(toPushOrderCarDetail:withTag:)]) {
        [self.Secdelegate toPushOrderCarDetail:self withTag:btn.tag];
    }
}
@end
